Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Twig] Add attributes() twig function #1414

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from

Conversation

kbond
Copy link
Member

@kbond kbond commented Jan 23, 2024

Q A
Bug fix? no
New feature? no
Issues n/a
License MIT

This allows passing attributes to elements easier.

<div{{ attributes({foo: 'bar'})>

When/if #1413 is merged, the following will also be possible:

<twig:Some:Component {{ ...attributes({foo: 'bar'}) }} />

@kbond
Copy link
Member Author

kbond commented Jan 23, 2024

Similar twig core issues/PRs: twigphp/Twig#3930, twigphp/Twig#3907, twigphp/Twig#3760

@kbond
Copy link
Member Author

kbond commented Jan 23, 2024

The function name attributes is unfortunate as it can be confused with the attributes parameter in twig/live components:

<div{{ attributes }}> {# correct #}

<div{{ attributes() }}> {# wrong #}

html_attr() maybe?

@Kocal
Copy link
Member

Kocal commented Jan 23, 2024

That's a great idea, it would also close #940 since the main idea was to easily renders attributes

Copy link
Collaborator

@WebMamba WebMamba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love that 🧡

@WebMamba
Copy link
Collaborator

The function name attributes is unfortunate as it can be confused with the attributes parameter in twig/live components:

Yes, I agree here. I like html_attr but also attr as it's proposed in the Twig issue.

How it's gonna work with Live components for example?

<div attributes({role: 'alert', class='text-blue-800 px-4',  ...attributes})>
...
</div>

@norkunas
Copy link
Contributor

Would love to have this in Twig itself :)

weaverryan added a commit that referenced this pull request Jan 29, 2024
… (kbond)

This PR was merged into the 2.x branch.

Discussion
----------

[Twig] Make `ComponentAttributes` traversable/countable

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| Issues        | n/a
| License       | MIT

This will help with #1405 and #1414.

Commits
-------

8040d9c feat(twig): make `ComponentAttributes` traversable/countable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants